<?php
/**
 *
 * Author: lirong
 * Date: 2017/10/26
 * Time: 21:40
 */

namespace Www\Controller;



use Think\Controller;

class JobController extends Controller{

    private $_beanstalk =null;

    public function __construct(){
        Vendor('Beanstalkd.Beanstalkd');
        $config =[
            'persistent'=>false,
            'host'=>'10.0.0.90',
            'port'=>11300,
            'timeout' =>1
        ];
        $this->_beanstalk =new \Beanstalkd($config);
    }


    public function put(){

        if (!$this->_beanstalk->connect()){
            exit("beanstalk not connect");
        }

        $this->_beanstalk->useTube("dt_dtmall_job");
        $data =json_encode(['id'=>1,'name'=>'lirong','password'=>md5('123456')]);
        $put =$this->_beanstalk->put(23,5,2,$data);

        if (!$put){
            exit('commit job fail');
        }
        $this->_beanstalk->disconnect();

    }

    public function watch(){

        if (!$this->_beanstalk->connect()){
            die("beanstalk is not connect");
        }

   //     var_dump($this->_beanstalk->stats());
//
        $listTubes =$this->_beanstalk->listTubes();
//
    //    var_dump($listTubes);

        $this->_beanstalk->watch("trj_orders_pay_check");

        $job =$this->_beanstalk->reserve();

        print_r($job);









        die();
        foreach ($listTubes as $listTube){
             echo $listTube;
////            $this->_beanstalk->useTube("trj_orders_pay_check");
////            $this->_beanstalk->watch("trj_orders_pay_check");
////
////            $this->_beanstalk->ignore('default');
////        //    $i=0;
////        //    while ($i<30){
////                $job =$this->_beanstalk->reserve();
////                print_r($job);
////        //        $i++;
////        //    }
//
       }
//        $this->_beanstalk->disconnect();


    }



    public function watch_bak(){

        if (!$this->_beanstalk->connect()){
            exit("beanstalk is not connnect");
        }
        //查看beanstalk状态
       // var_dump($this->_beanstalk->stats());
        //查看有多少个tube
       // var_dump($this->_beanstalk->listTubes());
       // exit();

        $this->_beanstalk->useTube("dt_dtmall_job");
        $this->_beanstalk->watch("dt_dtmall_job");

        $this->_beanstalk->ignore("default");

        while (true){
            $job =$this->_beanstalk->reserve();
            $result =$this->doJob($job['body']);
            if ($result){
                $this->_beanstalk->delete($job['id']);
            }else{
                $this->_beanstalk->bury($job['id']);
            }
            $file =APP_PATH."shutdown.txt";
            if (file_exists($file)){
                file_put_contents($file,"beanstalk在".date("Y-m-d H:i:s")."关闭");
                break;
            }

        }
        $this->_beanstalk->disconnect();
    }


    public function doJob($job){
        $app_file =APP_PATH."app.txt";
        file_put_contents($app_file,$job);
        if (!empty($job)){
            $data =json_decode($job);
            file_put_contents($app_file,$data);
            return true;
        }
        return false;
    }




}